package com.base.controller;


import com.base.comm.CommContent;
import com.base.po.voip.*;
import com.base.service.*;
import com.base.utils.Produce;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class add_CompanyArticle {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private CompanyClassifyService companyClassifyService;
	@Autowired
	private CompanyArticleService companyArticleService;
	@Autowired
	private UsersService usersService;

	// 账户信息
	@RequestMapping("/add_CompanyArticle")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		if (adminUserId == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
		
		Users users = usersService.selectByPrimaryKey(adminUserId);
		List<CompanyClassify> companyClassifies = null;
		if(users.getRole() == 0 || users.getRole() == 1) {
			companyClassifies = companyClassifyService.selectByAll(adminUserId);
		}
		
		request.setAttribute("companyClassifies", companyClassifies);
        
		modelAndView.setViewName("jsp/add_CompanyArticle");
		return modelAndView;
	}
	
	
	String SUCCESS = "success";

	// 添加商品
	@RequestMapping(value = "/insertCompanyArticle", method = RequestMethod.POST)
	public void insertSelfShopGoods(HttpServletRequest request, HttpServletResponse response, String title,
									String introduction, String classifyid, @RequestParam("yxAll") JSONArray yxAll,
									@RequestParam("ylAll") JSONArray ylAll, @RequestParam("jlAll") JSONArray jlAll,
									@RequestParam("hlAll") JSONArray hlAll, @RequestParam("activities") JSONArray activities,
									@RequestParam("propagates") JSONArray propagates, @RequestParam("gifts") JSONArray gifts) throws IOException {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String adminId = (String) request.getSession().getAttribute("adminUserId");
		if (adminId == null) {
			out.print("请刷新页面重试！");
			return;
		}

		// 营销效果
		String marketing = JSONArray.fromObject(yxAll).toString();

		// 活动现场
		String activity = JSONArray.fromObject(activities).toString();

		// 实操步骤
		Map<String, Object> scMap = new HashMap<>();
		scMap.put("drainageMap", JSONArray.fromObject(ylAll));
		scMap.put("closureMap", JSONArray.fromObject(jlAll));
		scMap.put("backflowMap", JSONArray.fromObject(hlAll));
		String training = JSONObject.fromObject(scMap).toString();

		// 宣传单
		String propagate = JSONArray.fromObject(propagates).toString();

		// 礼品架
		String gift = JSONArray.fromObject(gifts).toString();

		int resultCode = companyArticleService.insertSelective(new CompanyArticle(adminId, classifyid, title, introduction, marketing, activity,
				training, gift, propagate, Produce.getNowDate(), (byte)1));

		if (resultCode == 1) {
			out.print("发布成功");
		} else {
			out.print("发布失败");
		}
	}
}
